package com.kbbywyclient.ui.activity;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.RetrievePassWordOne;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.RetrievePwdOneModule;
import com.kbbywyclient.presenter.RetrievePwdOnePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.RetrievePwdOneContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.CheckFormat;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.widget.CustomToolBar;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 找回密码 第一步
 * Created by FHW
 * on 2017/3/31
 */

public class RetrievePwdOneActivity extends BaseActivity implements RetrievePwdOneContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.et_username)
    EditText et_username;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.iv_code)
    ImageView iv_code;
    @BindView(R.id.tv_next)
    TextView tv_next;

    @Inject
    RetrievePwdOnePresenter retrievePwdOnePresenter;

    private String imgToken = "";
    private String str_mobileOrEma = "";
    private String str_code = "";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_retrieve_pwd_one;
    }

    @OnClick({R.id.iv_refresh, R.id.tv_next})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_refresh:
                imgToken = new Date().getTime() + "";
                Picasso.with(this).load(retrievePwdOnePresenter.getPicCodeUrl(imgToken)).into(iv_code);
                break;
            case R.id.tv_next:
                if (vertify()) {
                    retrievePwdOnePresenter.getForgetPassWordOne(str_mobileOrEma, imgToken, str_code);
                }
                break;
        }
    }

    private boolean vertify() {
        if (str_mobileOrEma.isEmpty()) {
            ToastUtil.showShort("请输入手机号/邮箱");
            return false;
        }
        if (!(CheckFormat.isMobile(str_mobileOrEma) || CheckFormat.isEmail(str_mobileOrEma))) {
            ToastUtil.showShort("请输入正确的手机号/邮箱");
            return false;
        }
        if (str_code.isEmpty()) {
            ToastUtil.showShort("请输入验证码");
            return false;
        }
        if (!CheckFormat.isPicCode(str_code)) {
            ToastUtil.showShort("验证码输入错误！");
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("找回密码第一步");
    }

    @Override
    protected void initView() {
        v_toolbar.setLeftViewOnClickLinster(v -> {
            finish();
        });
        et_username.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_mobileOrEma = s.toString();
                isUserLoginFillIn();
            }
        });
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                str_code = s.toString();
                isUserLoginFillIn();
            }
        });
    }

    /**
     * 判断账号填写完成的操作
     */
    private void isUserLoginFillIn() {
        if ((CheckFormat.isMobile(str_mobileOrEma) || CheckFormat.isEmail(str_mobileOrEma)) && CheckFormat.isPicCode(str_code)) {
            tv_next.setBackgroundResource(R.drawable.textview_bg_14c095);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive()) {
                imm.hideSoftInputFromWindow(et_username.getWindowToken(), 0); //强制隐藏键盘
            }
        } else {
            tv_next.setBackgroundResource(R.drawable.textview_bg_dddddd);
        }
    }

    @Override
    public void initData() {
        imgToken = new Date().getTime() + "";
        Picasso.with(this).load(retrievePwdOnePresenter.getPicCodeUrl(imgToken)).into(iv_code);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getRetrievePwdOneComponent(new RetrievePwdOneModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucOne(RetrievePassWordOne retrievePassWordOne) {
        ActivitySkipUtil.skipAnotherActivity(this, RetrievePwdTwoActivity.class);
        EventBus.getDefault().postSticky(new Event.RetrievePwdOne(str_mobileOrEma));
    }

    @Override
    public void loadFaiOne(String errorMsg) {

    }
}
